/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cs.util.db.ioc;

import com.cs.util.db.ResultSetMetaInfo;
import com.cs.util.db.ioc.IOCExceptionState;
import com.cs.util.db.ioc.IOCException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * MySqlEnum is an IO controller for handling input and output of MySql ENUM columns.
 * @author dimitris@jmike.gr
 */
public class MySqlEnum extends MySqlChar implements IOController {

    String[] options;

    public MySqlEnum(ResultSetMetaInfo rsmi, int columnIndex) throws SQLException {
        super(rsmi, columnIndex);
        this.options = rsmi.getPossibleValues(columnIndex).split(",");
    }

    public MySqlEnum(ResultSetMetaInfo rsmi, String table, String column) throws SQLException {
        super(rsmi, table, column);
        this.options = rsmi.getPossibleValues(columnIndex).split(",");
    }

    /**
     * Returns a Xhtml form element for the controller's column.
     * @param rs the resultset containing the current row position and the field's value.
     * @param isEmpty a boolean flag that indicates whether this element contains data.
     * @return
     */
    @Override
    public String getXhtmlFormElement(ResultSet rs, boolean isEmpty) {
        StringBuilder s = new StringBuilder();
        s.append("<select size=\"1\"");
        final String id = this.getId();
        s.append(" id=\"" + id + "\"");
        s.append(" name=\"" + id + "\"");
        s.append(">");
        final String value;
        if (locked) {//input values are locked
            value = this.lockValue;
        } else {//input values are not locked
            if (rs != null && !isEmpty) {//resultset is not null and contains at least one row
                value = this.getValue(rs);
            } else {//resultset is empty
                value = this.getDefaultValue();//get default value
            }
        }
        boolean selected = false;
        for (int i = 0; i < options.length; i++) {//iterate options
            s.append("<option");
            s.append(" value=\"" + options[i] + "\"");
            if (!selected) {//an option has not yet been selected
                if (value != null && value.equals(options[i])) {//value equals this option
                    s.append(" selected=\"selected\"");//set option as selected
                    selected = true;
                }
            }
            s.append(">");
            s.append(options[i]);
            s.append("</option>");
        }
        s.append("</select>");
        return s.toString();
    }

    /**
     * Tries to validate the given value.
     * @param value the value as a String.
     * @return the correct value.
     * @throws IOCException
     */
    @Override
    public String validate(String value) throws IOCException {
        if (locked) {//input values are locked
            return lockValue;
        } else {//input values are not locked
            if (value != null && !value.isEmpty()) {//value is not null or empty
                for (int i = 0; i < options.length; i++) {//iterate options
                    if (options[i].equals(value)) {
                        return value;
                    }
                }
                throw new IOCException(table, column, IOCExceptionState.NOT_ACCEPTABLE);//value was not found in possible values
            } else {//value is null or empty
                if (isNullable) {//column excepts NULL values
                    return null;
                } else {//value cannot be null
                    throw new IOCException(table, column, IOCExceptionState.CANNOT_BE_EMPTY);
                }
            }
        }
    }
}
